package com.zcs.app.base.utils

import android.os.Looper
import android.util.Log
import top.zibin.luban.BuildConfig

object KLogUtil {
    const val TAG = "Log/ZengCS"
    const val TAG_STEP = "Debug/Step"
    const val TAG_DOWNLOAD = "Debug/Download"
    const val TAG_INSTALL = "Debug/Install"
    const val TAG_DOCTOR = "Debug/Doctor"
    const val TAG_HTTP = "Debug/Http"

    fun doctor(log: String) {
        i(TAG_DOCTOR, log)
    }

    fun logDownload(log: String) {
        i(TAG_DOWNLOAD, log)
    }

    fun logInstall(log: String) {
        i(TAG_INSTALL, log)
    }

    fun dev(tag: String?, msg: String) {
        if (BuildConfig.DEBUG) {
            i(tag, "[DEBUG]$msg")
        }
    }

    fun e(tag: String?, msg: String) {
        val isMain = Looper.myLooper() == Looper.getMainLooper()
        if (isMain) {
            Log.e(tag, "[@Main Thread]$msg")
        } else {
            Log.e(tag, "[Child Thread]$msg")
        }
    }

    fun e(e: Throwable?) {
        e("------------------------------------------------------------")
        e(Log.getStackTraceString(e))
        e("------------------------------------------------------------")
    }

    fun e(tag: String?, msg: String?, e: Throwable?) {
        Log.e(tag, msg, e)
    }

    fun w(tag: String?, msg: String) {
        val isMain = Looper.myLooper() == Looper.getMainLooper()
        if (isMain) {
            Log.w(tag, "[@Main Thread]$msg")
        } else {
            Log.w(tag, "[Child Thread]$msg")
        }
    }

    fun i(tag: String?, msg: String) {
        val isMain = Looper.myLooper() == Looper.getMainLooper()
        if (isMain) {
            Log.i(tag, "[@Main Thread]$msg")
        } else {
            Log.i(tag, "[Child Thread]$msg")
        }
    }

    fun d(tag: String?, msg: String) {
        val isMain = Looper.myLooper() == Looper.getMainLooper()
        if (isMain) {
            Log.d(tag, "[@Main Thread]$msg")
        } else {
            Log.d(tag, "[Child Thread]$msg")
        }
    }

    fun v(tag: String?, msg: String) {
        val isMain = Looper.myLooper() == Looper.getMainLooper()
        if (isMain) {
            Log.v(tag, "[@Main Thread]$msg")
        } else {
            Log.v(tag, "[Child Thread]$msg")
        }
    }

    // 无TAG方法
    fun e(msg: String) {
        e(TAG, msg)
    }

    fun e(msg: String?, e: Throwable?) {
        e(TAG, msg, e)
    }

    fun w(msg: String) {
        w(TAG, msg)
    }

    fun i(msg: String) {
        i(TAG, msg)
    }

    fun d(msg: String) {
        d(TAG, msg)
    }

    fun v(msg: String) {
        v(TAG, msg)
    }

    fun methodStart(msg: String) {
        w(TAG, "******************** Start $msg ********************")
    }

    fun methodStep(msg: String) {
        w(TAG, "* --> $msg")
    }

    fun methodStepWithTime(msg: String) {
        w(TAG_STEP, "[" + System.nanoTime() + "] --> " + msg)
    }

    fun methodStepError(msg: String) {
        e(TAG, "* !!! $msg")
    }

    fun methodEnd(msg: String) {
        w(TAG, "******************** End $msg ********************")
    }

    fun methodStartHttp(msg: String) {
        w(TAG_HTTP, "******************** Start $msg ********************")
    }

    fun methodStepHttp(msg: String) {
        w(TAG_HTTP, "* --> $msg")
    }

    fun onReceiveEvent(msg: String) {
        w(TAG, "【EventBus-Receive】$msg")
    }

    fun postEvent(msg: String) {
        w(TAG, "【EventBus-Post】$msg")
    }
}